<!-- pages/login/components/mobile.vue -->


<script lang="ts" >
import { getCodeApi, loginCodeAPi, } from '@/apis/user';

import { ref } from 'vue';
import { useUserStore } from '@/stores/user'
import { storeToRefs } from 'pinia';
export default {
  options: {
    styleIsolation: 'shared',
  },
  setup() {
    // 1. 倒计时组件的显示和隐藏
    const showCountDown = ref(false)
    // 1.1 获取验证码
    const getCode = async () => {
      try {
        //1.1.0校验手机号
        await formRef.value.validateField(['mobile'])
        // 1.1.1 显示倒计时组件
        showCountDown.value = true
        //1.1.2获取短信验证码
        const res = await getCodeApi({ mobile: formData.value.mobile, type: 'login' })
        formData.value.code=res.code
      } catch (error) {
        console.log(error);
        
      }

    }
    //2.登录业务
    const formRef = ref()
    //2.1定义数据对象
    const formData = ref({
      mobile:'',
      code:'',
    })
    //2.2定义校验规则对象
    const formRules = {
      mobile: {
        rules: [
          { required: true, errorMessage: '请填写手机号码' },
          { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
        ],
      },
      code: {
        rules: [
          { required: true, errorMessage: '请输入验证码' },
          { pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' },
        ],
      },
    }
    //2.3登录
     const isAgree = ref(false)
    const store=useUserStore()
    const onLogin = async () => {
      try {
        const data = await formRef.value.validate()

        // if(!isAgree.value) return uni.showToast({
        //   title: '请勾选协议用户协议',
        //   icon: 'none',

        // })
       // 调用接口函数
        const res = await loginCodeAPi(data)
        uni.showToast({ title: '登录成功', icon: 'success' })
        //把数据存储到仓库
        store.setUser(res)
        uni.switchTab({
          url:'/pages/my/index'
        })
       console.log(res);
       
        
      } catch (error) {
        console.log(error);

      }
    }


    return { showCountDown, getCode,formData,formRules,onLogin,formRef ,isAgree}
  }
  
}

</script>


<template>
  <uni-forms class="login-form" :model="formData" :rules="formRules" ref="formRef">
    <uni-forms-item name="mobile">
      <uni-easyinput v-model="formData.mobile" :input-border="false" :clearable="false" placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5" />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput v-model="formData.code" :input-border="false" :clearable="false" placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5" />
      <text class="text-button" v-if="!showCountDown" @click="getCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown :showDay="false" :showHour="false" :showMinute="false" :second="59"
          @timeup="showCountDown = false" />s后重新获取
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="false" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onLogin">登 录</button>
  </uni-forms>
</template>



<style lang="scss">
@import './styles.scss';
</style>